package com.linkallcloud.core.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.linkallcloud.core.domain.Domain;
import com.linkallcloud.core.lang.Strings;
import com.linkallcloud.core.www.ISessionCUser;
import com.linkallcloud.core.www.ISimpleUser;

public class Shadow implements Serializable {
    private static final long serialVersionUID = 771982781627593524L;

    private List<String> keys;
    private Map<String, Object> attrs;

    public Shadow() {
        super();
    }

    public Shadow(List<String> keys) {
        super();
        this.keys = keys;
    }

    public List<String> getKeys() {
        return keys;
    }

    public void setKeys(List<String> keys) {
        this.keys = keys;
    }

    public String[] keys() {
        if (this.keys == null || keys.isEmpty()) {
            return new String[0];
        }
        return this.keys.toArray(new String[this.keys.size()]);
    }

    public List<String> addKey(String key) {
        if (this.keys == null || keys.isEmpty()) {
            this.keys = new ArrayList<>();
        }
        this.keys.add(key);
        return this.keys;
    }

    public List<String> addKeys(List<String> addKeys) {
        if (this.keys == null || keys.isEmpty()) {
            this.keys = new ArrayList<>();
        }
        this.keys.addAll(addKeys);
        return this.keys;
    }

    public List<String> removeKey(String key) {
        if (this.keys != null && !keys.isEmpty()) {
            if (this.keys.contains(key)) {
                this.keys.remove(key);
            }
        }
        return this.keys;
    }

    public Map<String, Object> getAttrs() {
        return attrs;
    }

    public void setAttrs(Map<String, Object> attrs) {
        this.attrs = attrs;
    }

    public void addAttr(String name, Object value) {
        if (this.attrs == null) {
            this.attrs = new HashMap<String, Object>();
        }
        this.attrs.put(name, value);
    }

    public Object getAttr(String name) {
        if (this.attrs == null) {
            return null;
        }
        return this.attrs.get(name);
    }

    public void traceApp(Sid app) {
        if (app != null) {
            addAttr("appId", app.getId());
            addAttr("appUuid", app.getUuid());
            addAttr("appCode", app.getCode());
            addAttr("appName", app.getName());
        }
    }

    public void traceFss(Sid app, Domain d) {
        traceApp(app);
        if (d != null) {
            if (Strings.isBlank(d.getUuid())) {
                d.setUuid(d.generateUuid());
            }
            addAttr("objectId", d.getUuid());
        }
        if (app != null) {
            addAttr("objectType", app.getId() * 1000 + 1);
        }
    }

    public void traceUser(ISimpleUser su) {
        if (su != null) {
            addAttr("loginName", su.getLoginName());
            addAttr("userType", su.getUserType());
            addAttr("userClazz", su.getUserClazz());
            if (su instanceof ISessionCUser) {
                ISessionCUser csu = (ISessionCUser) su;
                addAttr("id", csu.id());
                addAttr("uuid", csu.uuid());
                addAttr("name", csu.name());
            }
        }
    }

}
